package com.sen.controller;


import com.sen.dto.BookDTO;
import com.sen.entity.Book;
import com.sen.result.Result;
import com.sen.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sen
 * @since 2021-03-13
 */
@Api(tags = "书籍控制器")
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @ApiOperation(value = "获取书籍列表")
    @GetMapping("/list/{current}/{size}")
    public Result getBookList(@PathVariable Integer current, Integer size){
        List<Book> bookList = bookService.getBookList(current, size);
        return Result.ok().data("data", bookList);
    }

    @ApiOperation(value = "新增书籍")
    @PostMapping
    public Result addBook(@RequestBody BookDTO bookDTO){
        boolean b = bookService.saveBook(bookDTO);
        if (b){
            return Result.ok().msg("添加成功");
        }
        else{
            return Result.error().msg("添加失败");
        }
    }

    @ApiOperation(value = "修改书籍")
    @PutMapping
    public Result updateBook(@RequestBody BookDTO bookDTO){
        boolean b = bookService.updateBook(bookDTO);
        if (b){
            return Result.ok().msg("更新成功");
        }
        else{
            return Result.error().msg("更新失败");
        }
    }

    @ApiOperation(value = "删除书籍")
    @DeleteMapping("/{id}")
    public Result deleteBookById(@PathVariable Integer id){
        boolean b = bookService.deleteBookById(id);
        if (b){
            return Result.ok().msg("删除成功");
        }
        else{
            return Result.error().msg("删除失败");
        }
    }

}
